package leetcode.剑指offer.剑指II;

import leetcode.util.CommonUtils;
import leetcode.剑指offer.pojo.ListNode;
import org.junit.Test;

import java.util.*;

/**
 * @author ：zsy
 * @date ：Created 2021/8/20 20:51
 * @description：https://leetcode-cn.com/problems/7WHec2/
 */
public class 剑指_II_77_链表排序 {
    @Test
    public void test() {
        Solution solution = new Solution();
        ListNode head = solution.sortList(new ListNode(4, new ListNode(3, new ListNode(1, new ListNode(2)))));
        CommonUtils.list_listNode(head);
    }

    class Solution {
        public ListNode sortList(ListNode head) {
            PriorityQueue<ListNode> priorityQueue = new PriorityQueue<>(Comparator.comparingInt(o -> o.val));
            ListNode temp = head;
            while (temp != null) {
                ListNode cur = temp;
                temp = temp.next;
                cur.next = null;
                priorityQueue.offer(cur);
            }
            ListNode curHead = new ListNode(-1);
            temp = curHead;
            while (!priorityQueue.isEmpty()) {
                temp.next = priorityQueue.poll();
                temp = temp.next;
            }
            return curHead.next;
        }
    }
}
